/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Connection;

import IFSv2.Models.mdlLogin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fulbert
 */
public class DBConnection {
    
    private static Connection conn = null;
    
    public static Connection connect() throws SQLException{
        
        String connString;
        
        String sqlServer = mdlLogin.sqlServer;
        String username = mdlLogin.dbUsername;
        String password = mdlLogin.dbPassword;
        String server = mdlLogin.dbServer;
        String dbName = mdlLogin.database;
        String port = mdlLogin.dbPort;
        
        switch (sqlServer) {
            case "MSSQL":
                connString = "jdbc:sqlserver://"+server+";database="+dbName+";"; 
                break;
            case "MySQL":
                connString = "jdbc:mysql://"+server+":"+port+"/"+dbName+"?zeroDateTimeBehavior=convertToNull";
                break;
            default:
                connString = "";
                break;
        }
        conn = DriverManager.getConnection(connString,username,password);
        conn.setAutoCommit(false);
        return conn;
    }

    public static Connection getActiveConnection(){
        try {
            if(conn != null && !conn.isClosed()) 
                return conn;
            else 
                connect();
        } catch (SQLException ex) {
        }
        
        return conn;
    }
    
}
